package cn.com.yusys.yusp.admin.web.rest;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.yusys.yusp.admin.service.AdminUserService;
import cn.com.yusys.yusp.admin.service.UserProviderService;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;

/**
 * 迁移yusp-app-message-core里MessageResource类中部分代码
 * 
 * @author Cytus_
 * @since 2019年1月22日 下午4:15:40
 * @version 1.0
 */
@RestController
@RequestMapping("/api")
public class AdminUserResource {
    
    private final static Logger logger = LoggerFactory.getLogger(AdminUserResource.class);
    
    @Autowired
    private AdminUserService userService;
    
    @Autowired
    private UserProviderService providerService;

    /**
     * @param queryModel
     * @return 查询所有用户
     * @update 添加total+message和异常处理
     */
    @GetMapping("/selectAllUser")
    protected ResultDto<List<Map<String, Object>>> selectAllUser(QueryModel queryModel) {
        logger.debug("Get all users!");
        ResultDto<List<Map<String, Object>>> resultDto = new ResultDto<>();
        try {
            List<Map<String, Object>> list = userService.selectAllUser(queryModel);
            resultDto.setData(list);
            resultDto.setMessage("获取用户成功!");
            resultDto.setTotal(list != null ? list.size() : 0);
        } catch (Exception e) {
            resultDto.setCode(500);
            resultDto.setMessage(e.getMessage());
        }

        return resultDto;
    }

    /**
     * @param queryModel
     * @return 查询所有机构
     * @update 添加total+message和异常处理
     */
    @GetMapping("/selectAllOrg")
    protected ResultDto<List<Map<String, Object>>> selectAllOrg(QueryModel queryModel) {
        logger.debug("获取所有机构!");
        ResultDto<List<Map<String, Object>>> resultDto = new ResultDto<>();
        try {
            List<Map<String, Object>> list = userService.selectAllOrg(queryModel);
            resultDto.setData(list);
            resultDto.setMessage("Get all organizations!");
            resultDto.setTotal(list != null ? list.size() : 0);
        } catch (Exception e) {
            resultDto.setCode(500);
            resultDto.setMessage(e.getMessage());
        }
        return resultDto;
    }

    @GetMapping("/selectAllRole")
    protected ResultDto<List<Map<String, Object>>> selectAllRole(QueryModel queryModel) {
        logger.debug("Get all roles!");
        ResultDto<List<Map<String, Object>>> resultDto = new ResultDto<>();
        try {
            List<Map<String, Object>> list = userService.selectAllRole(queryModel);
            resultDto.setData(list);
            resultDto.setMessage("获取角色成功!");
            resultDto.setTotal(list != null ? list.size() : 0);
        } catch (Exception e) {
            resultDto.setCode(500);
            resultDto.setMessage(e.getMessage());
        }
        return resultDto;
    }

    @GetMapping("/selectAllDuty")
    protected ResultDto<List<Map<String, Object>>> selectAllDuty(QueryModel queryModel) {
        logger.debug("Get all jobs!");
        ResultDto<List<Map<String, Object>>> resultDto = new ResultDto<>();
        try {
            List<Map<String, Object>> list = userService.selectAllDuty(queryModel);
            resultDto.setData(list);
            resultDto.setMessage("获取岗位成功!");
            resultDto.setTotal(list != null ? list.size() : 0);
        } catch (Exception e) {
            resultDto.setCode(500);
            resultDto.setMessage(e.getMessage());
        }
        return resultDto;
    }
    
    /***********************
     * 以下代码为迁移后新增，为了配合yusp-app-message-core使用
     *************************/
    
    /**
     * 通过机构查询该机构下所有用户
     * @param orgId
     * @return
     */
    @GetMapping(value = "/user/org/{orgId}")
    public List<String> queryUserByOrgId(@PathVariable("orgId") String orgId) {
        return providerService.queryUserByOrgId(orgId);
    }
    
    /**
     * 通过机构id查询所属机构下用户信息
     * @param orgIds
     * @return
     */
    @PostMapping(value = "/user/orgs")
    public List<String> queryUserByOrgIds(@RequestBody List<String> orgIds) {
        return providerService.queryUserByOrgIds(orgIds);
    }
    
    /**
     * 通过角色id查询属于该角色的所有用户
     * @param roleId
     * @return
     */
    @GetMapping(value = "/user/role/{roleId}")
    public List<String> queryUserByRoleId(@PathVariable("roleId") String roleId) {
        return providerService.queryUserByRoleId(roleId);
    }
    
    /**
     * 通过部门id查询属于该部门下所有的用户信息
     * @param dutyId
     * @return
     */
    @GetMapping(value = "/user/duty/{dutyId}")
    public List<String> queryUserByDutyId(@PathVariable("dutyId") String dutyId) {
        return providerService.queryUserByDutyId(dutyId);
    }
    
    /**
     * 通过用户id查询该用户所属机构id
     * @param userId
     * @return
     */
    @GetMapping(value = "/org/user/{userId}")
    public List<String> queryOrgsByUserId(@PathVariable("userId") String userId) {
        return providerService.queryOrgsByUserId(userId);
    }
    
    /**
     * 通过上级机构id查询所有的上级机构id属于该上级机构id
     * @param upOrgId
     * @return
     */
    @GetMapping(value = "/org/uporg/{upOrgId}")
    public List<String> queryOrgsByUpOrgId(@PathVariable("upOrgId") String upOrgId) {
        return providerService.queryOrgsByUpOrgId(upOrgId);
    }
    
    /**
     * 根据机构id查询该机构的上级机构
     * @param orgId
     * @return
     */
    @GetMapping(value = "/uporg/org/{orgId}")
    public List<String> queryUpOrgByOrgId(@PathVariable("orgId") String orgId) {
        return providerService.queryUpOrgByOrgId(orgId);
    }
    
    /**
     * 根据用户id查询用户信息
     * @param userId
     * @return
     */
    @GetMapping(value = "/user/{userId}")
    public Map<String, Object> queryUserInfoByUserId(@PathVariable("userId") String userId) {
        return providerService.queryUserInfoByUserId(userId);
    }
    
}
